package example.audio.track.repository.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import example.audio.track.Track;
import example.audio.track.repository.TrackRepository;

@Repository
public class TrackHibernateDao implements TrackRepository {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Track addTrack(Track track) {
		sessionFactory.getCurrentSession().save(track);
		return track;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Track> getAllTracks() {
		return sessionFactory.getCurrentSession().createQuery("from Track").list();
	}

	@Override
	public Track getTrackById(Integer id) {
		return (Track) sessionFactory.getCurrentSession().get(Track.class, id);
	}

	@Override
	public Track updateTrack(Track track) {
		sessionFactory.getCurrentSession().update(track);
		return track;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Track> getConditionalTracks(Map<String, Object> params) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Track.class);		
		return criteria.add(Expression.allEq(params)).list();
	}

}
